<?php
/**
 * <pre>
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		    : x4you
 * COPYRIGHT	: 2009
 * ------------------------------------------------------
 * 
 * [GENERAL INFO OF THIS FILE] 
 * 
 * </pre>
 * 
 * Created on	: May 28, 2009
 * 
 * @author		hoadn <hoadn@rubik.com.vn>
 * @access		[public/private]
 * @package	[package name]
 *
 */
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );
RClassesImporter::cls('dbo.dtos.UserDTO');

class UserModel {
	
	function saveDTO(&$dto) {
		return SysDatabase::saveDTO($dto);
	}
	
	/**
	 * Load the userDTO
	 * @param $id
	 * @return UserDTO
	 */
	function loadUserDTO($id) {
		$dto = new UserDTO();
		$dto->setKeyValue($id);
		SysDatabase::fillData($dto);
		return $dto;
	}

	/**
	 * 
	 * @param $dto AbstractDTO
	 * @return mixed
	 */
	function upsertDTOByKey(&$dto) {
		$key = $dto->getKeyName();
		if ($key) {
			return $dto->upsert($key);
		}
		return false;
	}
	
	function getModel($modelName) {
		RClassesImporter::cls('dbo.finders.ModelListFinder');
		$finder = new ModelListFinder();
		$finder->user_name = $modelName;
		
		$sql = $finder->getSelectSQL();
		return SysDatabase::loadObject($sql);
	}
	
	/**
	 * 
	 * @param $userId
	 * @param $amount
	 * @param $status
	 * @param $reason
	 * @return PaymentTransationDTO
	 */
	function savePaymentHistoryForSite($userId, $amount, $status, $transId, $reason) {
		$userDto = $credit_remain = $this->loadUserDto($userId);
		RClassesImporter::cls('dbo.dtos.PaymentTransationDTO');
		$dto = new PaymentTransationDTO();
		$dto->user_id 	= $userId;
		$dto->amount	= $amount;
		$dto->credit_remain = $userDto->credit_remain + $amount;
		$dto->status	= $status;
		$dto->reason 	= $reason;
		$dto->trans_id 	= $transId;
		SysDatabase::saveDTO($dto);
		return $dto;
	}
}